package org.agilest.demo.gallary;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;

/**
 * Created by david on 15/11/7.
 */
public class CoverFlow extends Gallery {

	private Camera mCamera = new Camera();
	private static final int mMaxRotationAngle = 80;

	public CoverFlow(Context context) {
		super(context);
		this.setStaticTransformationsEnabled(true);
	}

	public CoverFlow(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setStaticTransformationsEnabled(true);
	}


	public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.setStaticTransformationsEnabled(true);
	}


	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		int kEvent = 0;
		if(isScrollingLeft(e1,e2)){
			kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
		}else{
			kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
		}
		onKeyDown(kEvent,null);

		return true;
	}
	private boolean isScrollingLeft(MotionEvent e1,MotionEvent e2){
		return e2.getX()>e1.getX();
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return super.onScroll(e1, e2, distanceX*0.2f, distanceY);
	}

	public void next(){
		View view = this.getSelectedView();
		int index = Integer.parseInt(view.getTag().toString());
		this.setSelection(index,false);
		this.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT,null);
	}


	protected boolean getChildStaticTransformation(View child, Transformation t) {
		t.clear();
		t.setTransformationType(Transformation.TYPE_MATRIX);

		int childIndex = Integer.parseInt(child.getTag().toString());
		int currentIndex = Integer.parseInt(this.getSelectedView().getTag().toString());
		if(childIndex==currentIndex){
			transformImageBitmap((ImageView) child,t,0);
		}else if(childIndex<currentIndex){
			transformImageBitmap((ImageView) child,t,mMaxRotationAngle);
		}else{
			transformImageBitmap((ImageView) child,t,-mMaxRotationAngle);
		}

		return true;
	}


	private void transformImageBitmap(ImageView child, Transformation t,
			int rotationAngle) {
		mCamera.save();
		final Matrix imageMatrix = t.getMatrix();
		final int imageHeight = child.getLayoutParams().height;
		final int imageWidth = child.getLayoutParams().width;
		final int rotation = Math.abs(rotationAngle);

//		在Z轴上正向移动camera的视角，实际效果为放大图片。
//		如果在Y轴上移动，则图片上下移动；X轴上对应图片左右移动。
		mCamera.translate(0.0f, 0.0f, 100.0f);


//		在Y轴上旋转，对应图片竖向向里翻转。
//		如果在X轴上旋转，则对应图片横向向里翻转。
		mCamera.rotateY(rotationAngle);
		mCamera.getMatrix(imageMatrix);
		imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
		imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
		mCamera.restore();
	}
}
